package com.hongxun.common.utils;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import com.google.gson.Gson;

/**
 * Rest工具类
 *
 */
//http://rensanning.iteye.com/blog/2362105
@ConfigurationProperties(prefix = "platform.rest.rtuServer")
@Component
public class RestUtils {
	
	private String rtuUsername;
    private String rtuPassword;
    private String rtuUrl;
    private String rtuLoginpath;
    
    private String serviceUsername;
    private String servicePassword;
    private String serviceUrl;
    private String serviceLoginpath;   
    
	


	public String getRtuUsername() {
		return rtuUsername;
	}

	public void setRtuUsername(String rtuUsername) {
		this.rtuUsername = rtuUsername;
	}

	public String getRtuPassword() {
		return rtuPassword;
	}

	public void setRtuPassword(String rtuPassword) {
		this.rtuPassword = rtuPassword;
	}

	public String getRtuUrl() {
		return rtuUrl;
	}

	public void setRtuUrl(String rtuUrl) {
		this.rtuUrl = rtuUrl;
	}

	public String getRtuLoginpath() {
		return rtuLoginpath;
	}

	public void setRtuLoginpath(String rtuLoginpath) {
		this.rtuLoginpath = rtuLoginpath;
	}

	public String getServiceUsername() {
		return serviceUsername;
	}

	public void setServiceUsername(String serviceUsername) {
		this.serviceUsername = serviceUsername;
	}

	public String getServicePassword() {
		return servicePassword;
	}

	public void setServicePassword(String servicePassword) {
		this.servicePassword = servicePassword;
	}

	public String getServiceUrl() {
		return serviceUrl;
	}

	public void setServiceUrl(String serviceUrl) {
		this.serviceUrl = serviceUrl;
	}

	public String getServiceLoginpath() {
		return serviceLoginpath;
	}

	public void setServiceLoginpath(String serviceLoginpath) {
		this.serviceLoginpath = serviceLoginpath;
	}

	@Autowired
	private RestTemplate restTemplate;
	
	public String loginRtu() {
		// TODO Auto-generated method stub
		final MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
		params.add("Name", rtuUsername);
		params.add("Password", rtuPassword);
		String json = restTemplate
				.postForEntity(rtuUrl + rtuLoginpath, params, String.class)
				.getBody();
		
		Map map = new Gson().fromJson(json, Map.class);
		String result = "" + map.get("code");
		return result;
	}
	
	public String getRtuData(String path) {
		// TODO Auto-generated method stub
		  
		System.out.println("rtuUsername:"+rtuUsername);
		System.out.println("rtuPassword:"+rtuPassword);
		System.out.println("rtuUrl:"+rtuUrl);
		System.out.println("rtuLoginpath:"+rtuLoginpath);
		System.out.println("serviceUsername:"+serviceUsername);
		System.out.println("servicePassword:"+servicePassword);
		System.out.println("serviceUrl:"+serviceUrl);
		System.out.println("serviceLoginpath:"+serviceLoginpath);
		
		String json = restTemplate.getForObject(rtuUrl + path, String.class);
		Map map = new Gson().fromJson(json, Map.class);
		String result = "" + map.get("code");
		if (Constant.RTU_LOGIN_ERROR.equals(result)) {
			this.loginRtu();
			String reJson = getRtuData(rtuUrl);
			return reJson;
		}
		return json;
	}
}
